﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Xml;

namespace BLL
{
    public class ZipCalculator
    {
        #region Constructor

        public ZipCalculator()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        #endregion Constructor

        #region Properties

        private static int ZIPCALC_ORIGIN
        {
            get
            {
                return Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["ZIPCALC_ORIGIN"]);
            }
        }

        private static string ZIPCALC_MODE
        {
            get
            {
                return System.Configuration.ConfigurationManager.AppSettings["ZIPCALC_MODE"];
            }
        }

        private static string ZIPCALC_LANGUAGE
        {
            get
            {
                return System.Configuration.ConfigurationManager.AppSettings["ZIPCALC_LANGUAGE"];
            }
        }

        private static string ZIPCALC_SENSOR
        {
            get
            {
                return System.Configuration.ConfigurationManager.AppSettings["ZIPCALC_SENSOR"];
            }
        }

        private static string ZIPCALC_UNITS
        {
            get
            {
                return System.Configuration.ConfigurationManager.AppSettings["ZIPCALC_UNITS"];
            }
        }

        #endregion Properties

        #region Methods

        /// <summary>
        /// 
        /// </summary>
        /// <example>http://maps.googleapis.com/maps/api/distancematrix/xml?origins=98004&destinations=98007&mode=driving&language=en-US&sensor=false&units=imperial</example>
        /// <param name="originZip"></param>
        /// <param name="destinationZip"></param>
        /// <returns></returns>
        public static double GetDistance(int destinationZip)
        {
            double returnValue = 0;

            if (ZIPCALC_ORIGIN > 0 && destinationZip > 0 && !string.IsNullOrEmpty(ZIPCALC_MODE) &&
                !string.IsNullOrEmpty(ZIPCALC_LANGUAGE) && !string.IsNullOrEmpty(ZIPCALC_SENSOR) &&
                !string.IsNullOrEmpty(ZIPCALC_UNITS))
            {
                XmlDocument doc = new XmlDocument();

                doc.Load(string.Concat("http://maps.googleapis.com/maps/api/distancematrix/xml?origins=",
                    ZIPCALC_ORIGIN, "&destinations=", destinationZip, "&mode=", ZIPCALC_MODE, "&language=", ZIPCALC_LANGUAGE,
                    "&sensor=", ZIPCALC_SENSOR, "&units=", ZIPCALC_UNITS));

                XmlNodeList nodes = doc.SelectNodes("/DistanceMatrixResponse/row/element/distance/value");

                foreach (XmlNode node in nodes)
                {
                    returnValue = Convert.ToDouble(node.InnerText);
                    break;
                }

                returnValue = CalculateMiles(returnValue / 1000);
            }

            return returnValue;
        }

        private static double CalculateMiles(double kilometer)
        {
            double returnValue = 0;

            if (kilometer > 0)
            {
                returnValue = kilometer * 0.621371192;
            }

            return returnValue;
        }

        private static double CalculateKiloMeter(double miles)
        {
            double returnValue = 0;

            if (miles > 0)
            {
                returnValue = miles * 1.609344;
            }

            return returnValue;
        }

        #endregion Methods
    }
}
